// if (window.__POWERED_BY_QIANKUN__) {
//   // eslint-disable-next-line no-undef
//   __webpack_public_path__ = window.__INJECTED_PUBLIC_PATH_BY_QIANKUN__
// }

import Vue from 'vue'

import Cookies from 'js-cookie'
import 'normalize.css/normalize.css' // a modern alternative to CSS resets

import Element from 'element-ui'
import './styles/element-variables.scss'
import './styles/gis-ui.scss' // gis css cover

import '@/styles/index.scss' // global css
import '@/styles/sysm.scss' // global css
import App from './App'
import store from './store'
import router from './router'
import './icons' // icon
import './utils/error-log' // error log
import './utils/public' // public method and config
import './permission' // permission control
import actions from '@/utils/actions'

import permissionButton from './directive/permission-button' // button permission
Vue.use(permissionButton)

import components from '@/components'
Vue.use(components)

import 'viewerjs/dist/viewer.css' // image preview
import Viewer from 'v-viewer'
Vue.use(Viewer, {
  defaultOptions: {
    zIndex: 9999,
    title: val => {
      return val.alt
    }
  }
})

/**
 * If you don't want to use mock-server
 * you want to use MockJs for mock api
 * you can execute: mockXHR()
 *
 * Currently MockJs will be used in the production environment,
 * please remove it before going online! ! !
 */
import { mockXHR } from '../mock'
if (process.env.NODE_ENV === 'production') {
  mockXHR()
}
Vue.use(Element, {
  size: Cookies.get('size') || 'medium' // set element-ui default size
})

Vue.config.productionTip = false

let vm
let subDiv

function render(props = {}) {
  vm = new Vue({
    router,
    store,
    render: h => h(App)
  }).$mount('#app')

  if (window.__POWERED_BY_QIANKUN__) {
    if (process.env.NODE_ENV === 'development') {
      // 作为子应用运行时 兼容vueDevTools的调试
      subDiv = document.createElement('div')
      subDiv.__vue__ = vm
      document.body.appendChild(subDiv)
    }

    const { parentRouter, theme } = props
    Vue.prototype.parentRouter = parentRouter

    actions.setActions(props)

    switch (theme) {
      case 'dark':
        import('@/styles/theme/dark.scss')
    }
  }
}

// 独立运行时
if (!window.__POWERED_BY_QIANKUN__) {
  render()
}

export async function bootstrap() {}

export async function mount(props) {
  render(props)
}

export async function unmount() {
  store.dispatch('permission/clearRoutes')
  vm.$destroy()
  vm.$el.innerHTML = ''
  vm = null
  if (subDiv) {
    document.body.removeChild(subDiv)
  }
}
